package com.iseastar.guojiang.cabinet;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.iseastar.BaseActivity2;
import com.iseastar.guojiang.app.AppCaptureActivity;
import com.iseastar.guojiang.cabinet.adapter.CabinetParcelsListAdapter;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.CabinetBean;
import com.iseastar.guojiang.model.OrderBean;
import com.iseastar.guojiang.model.ReqResult;
import com.shouhuobao.station.R;

import droid.frame.view.xlist.SListView;

/**
 * 智能柜、包裹列表
 *
 * @author coffee <br>
 *         2016-8-3下午2:25:14
 */
public class CabinetParcelsListActivity extends BaseActivity2 implements View.OnClickListener {

    private SListView mListView;
    private CabinetParcelsListAdapter mAdapter;

    private CabinetBean.StationCabubetBean item;
    private OrderBean clickItem;
    private String parcelId;//要绑定袋子的包裹ID

    @Override
    @SuppressLint("ResourceAsColor")
    protected void onCreate(Bundle savedInstanceState) {
        item = (CabinetBean.StationCabubetBean) getIntent().getSerializableExtra("item");
        super.onCreate(savedInstanceState);
        showLoadingDialog(null);
        AppHttp.getInstance().cabinetGetById(item.getId(), item.getCheckCode());
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.cabinet_grid);
        super.findViewById();
        getAppTitle().setCommonTitle("包裹列表");
        mListView = (SListView) findViewById(R.id.listview);
        mListView.setFooterDividersEnabled(true);
        mAdapter = new CabinetParcelsListAdapter(null, this, mListView);
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                clickItem = (OrderBean) parent.getItemAtPosition(position);
                Intent intent = new Intent();
                intent.setClass(getContext(), CabinetParcelDetailActivity.class);
                intent.putExtra("item", clickItem);
                startActivityForResult(intent, 101);
            }
        });
        mSwipeLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                AppHttp.getInstance().cabinetGetById(item.getId(), item.getCheckCode());
            }
        });
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            // 检验袋子、并揽收
            case MsgID.cabinet_evaluate_bagcode:
                cancelLoadingDialog();
                ReqResult<String> result1 = JSON.parser(msg.obj, String.class);
                if (checkLoginStatus(result1)) {
                    showToast(result1.getMessage());
                    mAdapter.getItems().remove(clickItem);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mAdapter.notifyDataSetChanged();
                        }
                    });
                } else {
                    showToast(result1.getMessage());
                }
                return true;
            // 获取柜子下面的订单列表
            case MsgID.cabinetGetById:
                onRefreshComplete();
                cancelLoadingDialog();
                final ReqResult<OrderBean> result = JSON.parseList(msg.obj, OrderBean.class);
                if (checkLoginStatus(result)) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mAdapter.setItems(result.getResultList(), true);
                        }
                    });
                } else {
                    showToast(result.getMessage());
                }
                return true;
            // 拒收包裹
            case MsgID.order_refuse:
                ReqResult<String> result2 = JSON.parser(msg.obj, String.class);
                if (checkLoginStatus(result2)) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mAdapter.getItems().remove(clickItem);
                            mAdapter.notifyDataSetChanged();
                        }
                    });
                }
                return false;//
            //进行袋子和包裹绑定
            case MsgID.bagcode_bindparcel:
                cancelLoadingDialog();
                ReqResult<?> result3 = JSON.parser(msg.obj);
                if (checkLoginStatus(result3)) {
                    showToast("绑定成功");
                    showLoadingDialog(null);
                    AppHttp.getInstance().cabinetGetById(item.getId(), item.getCheckCode());
                } else {
                    showToast(result3.getMessage());
                }
                return true;
            default:
                return super.handleMessage(msg);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            // 验收包裹
            case R.id.cabinet_check:
                clickItem = (OrderBean) v.getTag();
                Intent intent = new Intent();
                //没有绑定袋子编号，跳转到绑定袋子页面
                if (TextUtils.isEmpty(clickItem.getBagCode()) || "null".equals(clickItem.getBagCode())) {
                    parcelId = clickItem.getParcelId();
                    intent.setClass(getContext(), AppCaptureActivity.class);
                    startActivityForResult(intent, 103);

                } else {
                    intent.setClass(getContext(), AppCaptureActivity.class);
                    startActivityForResult(intent, 102);
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != RESULT_OK) {
            return;
        }
        if (requestCode == 102) {
            String result = data.getStringExtra("result");
            if (isEmpty(result)) {
                return;
            }
            int index = result.lastIndexOf("=");
            String code2;
            if (index > -1) {
                code2 = result.substring(index + 1, result.length());
            } else {
                code2 = result;
            }
            // 验证通过
            if (code2.equals(clickItem.getBagCode())) {
                showLoadingDialog(null);
                // AppHttp.getInstance().cabinetEvaluateBagCode(clickItem.getBagCode(), clickItem.getId(), 2);
                AppHttp.getInstance().collectBoxParcel(clickItem.getBagCode(), clickItem.getParcelId());
            } else {
                showToast("验证失败");
            }
        } else if (requestCode == 103) {
            String bagcode = data.getStringExtra("result");
            if (isEmpty(bagcode)) {
                return;
            }
            int index = bagcode.lastIndexOf("=");
            String code2;
            if (index > -1) {
                code2 = bagcode.substring(index + 1, bagcode.length());
            } else {
                code2 = bagcode;
            }
            showLoadingDialog(null);
            AppHttp.getInstance().bagCodeBindParcel(code2, parcelId);
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

}
